package com.swt.spider;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

public class HttpSpider {
    public static void main(String[] args) {

//        JSONObject object = new JSONObject();
//        object.set("orgCode", "");
//        object.set("taskType", "");
//        object.set("regionCode", "420000000000");
//        object.set("grade","2");
//        object.set("currentPage","2");
//        object.set("pageSize","10");
//        object.set("onlyLocalLevel","1");
//        object.set("onlyGrfRSearch","0");
//        object.set("handlingtimes","");
        Map<String, Object> paramData = new HashMap<>();
        paramData.put("orgCode", "");
        paramData.put("taskType", "");
        paramData.put("regionCode", "420000000000");
        paramData.put("grade", "2");
        paramData.put("pageSize", 10);
        paramData.put("onlyLocalLevel", "1");
        paramData.put("onlyGrfRSearch", "0");
        paramData.put("handlingtimes", "");


        int page = 320;

        for (int i = 1; i < page; i++) {
            paramData.put("currentPage", i);
            String httpRequest = HttpRequest.post("http://zwfw.hubei.gov.cn/web/bszn/zwgk/qzqd/getQzqdItemList.jspc")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0")
                    .form(paramData)
                    .execute()
                    .body();


            JSONObject jsonObject = JSONUtil.parseObj(httpRequest);
            JSONObject data = jsonObject.getJSONObject("data");
            JSONObject pageList = data.getJSONObject("pageList");
            System.err.println(StrUtil.format("-------------第{}页返回结果------------------", i));
            System.out.println(pageList);
            FileUtil.writeUtf8String(pageList.toStringPretty(), "C:\\Users\\xiaow\\Desktop\\csv\\" + i + ".json");
            ThreadUtil.sleep(1000);
        }







       /* JSONArray records = pageList.getJSONArray("records");
        CsvWriter writer = new CsvWriter("E:\\hubei\\2.csv");
        for (int i = 0; i < records.size(); i++) {
            String [] strings=new String[2];
            JSONObject obj= records.getJSONObject(i);
            String taskcode = obj.getStr("TASKCODE");
            String mulu = obj.getStr("MULU");
            strings [0]=taskcode;
            strings [1]=mulu;
            writer.write(strings);
        }
        writer.close();
        System.out.println(records.toStringPretty());*/

    }
}
